Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

295
Views
"#define something something" in C

I'm looking at some microcontroller C headers and there is code like this:

#define OSCCONL OSCCONL
extern volatile uint8_t OSCCONL __attribute__((__sfr__));
#define OSCCONH OSCCONH
extern volatile uint8_t OSCCONH __attribute__((__sfr__));
#define CLKDIV CLKDIV
extern volatile uint16_t CLKDIV __attribute__((__sfr__));
__extension__ typedef struct tagCLKDIVBITS {...}

What is the purpose of those repetitive "#define A A" ?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

As the preprocessor does not know anything about the C language and variables you need to add some definitions know if the variable (variable) will be defined in the C code. You can use those definitions later in the code.

Example:

#if defined(OSCONL)
OSCONL = something;
#endif

And if your microcontroller does not have this register this part of the code will not be compiled. It is a very common technique.

EDIT:

But wouldn't that work the same if it just had #define OSCCONL

The idea is to have the same #define as variable or function name. It makes code consistent and easy to remember.

Otherwise, you would have to have different #define and object name making coding more difficult, as our would have to remember both. In this convention, you can read the documentation of the uC and use the same name to check if this object exists without remembering how the #define is called.

over 4 years ago · Santiago Trujillo Report

0

One purpose could be that it can be used in a macro check such as #ifdef or #ifndef

This allows for conditional compiling where only parts of the source code will be compiled into the binary.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!